package com.subway.babynote;

/*水*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.subway.babynote.syncdata.ClientDataRecord.contactRecord;
import com.subway.babynote.syncdata.ClientDataRecord.foodsRecord;
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class choosebrand extends Activity {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_BRAND = 3;
	String[] food_array;
	NotedbHelper notedb;
	FocusListView lv;
	Cursor cur;
	int col, food_id;
	String food_name,category;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;
	Button login_button;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choosebrand);
		setTitle(R.string.note);
		notedb = new NotedbHelper(this);
		In = new Intent();
		//initTabFunction();
		food_array = getResources().getStringArray(R.array.food_category);
		lv = (FocusListView) findViewById(R.id.choosebrand_list);
	
		login_button = (Button) findViewById(R.id.login_button);
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			Bundle bundle = this.getIntent().getExtras();
			category=bundle.getString("category");
			// first check if it is milk
		     Log.v("choosebrand",food_array[1]);
			if (bundle.getString("from").equals("feedcategory")) {
				if (category.equals(food_array[1])) {		
				//	cur = notedb.getFeedsbyCategory(bundle.getString("category"));
				//	syncapi.getSyncImpl().getFoodybyCategory(category);
					if (syncapi.getSyncImpl().getFoodybyCategory(category)!=null) {
						//this.startManagingCursor(cur);
						//col = cur.getColumnIndex("food_id");
					//	food_id = syncapi.getSyncImpl().getFoodybyCategory(category);
						col = cur.getColumnIndex("name");
						food_name = cur.getString(col);
						bundle.putInt("food_id", food_id);
						bundle.putString("foodname",
								bundle.getString("category") + ">>" + food_name);
						In.setClass(choosebrand.this, inputamount.class);
						In.putExtras(bundle);
						startActivityForResult(In,RG_BRAND);
					}
				}
			}
			//cur = notedb.getFoodybyCategory(bundle.getString("category"));
			List<foodsRecord> lc=syncapi.getSyncImpl().getFoodybyCategory(bundle.getString("category"));
			ArrayList<HashMap<String, Object>> foods = new ArrayList<HashMap<String, Object>>();
		/*	ListAdapter listAdapter = new SimpleCursorAdapter(this,
					R.layout.list_item, cur, new String[] { "foodname" },
					new int[] { R.id.list_item });*/

			Iterator<foodsRecord> it = lc.iterator();
			while(it.hasNext()){
				HashMap<String, Object>  food= new HashMap<String, Object>();
				food.put("foodname", it.next().foodname);				
				foods.add(food);
			}
			ListAdapter listAdapter = new SimpleAdapter(this, foods, R.layout.list_item,new String[]{"foodname"}, new int[]{R.id.list_item});	
			lv.setAdapter(listAdapter);
		} catch (Exception e) {
			Log.v("choosebrand", " e=" + e.getMessage());
		}
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				In.setClass(choosebrand.this, login.class);
				startActivity(In);
			}
		});

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Bundle bundle = new Bundle();
				// try to get food_id
				try {

					ListView listview = (ListView) parent;
					Cursor cur = (Cursor) listview.getItemAtPosition(position);
					col = cur.getColumnIndex("foodname");
					Log.v("choosebrand", "col" + col);
					if (!cur.isNull(col)) {
						String foodname = cur.getString(col);
						Log.v("choosebrand", "foodname=" + foodname);
						int food_id = syncapi.getSyncImpl().getFood(foodname).food_id;  
						bundle.putInt("food_id", food_id);
						bundle.putString("foodname",
								category + ">>" + foodname);
						In.setClass(choosebrand.this, inputamount.class);
						In.putExtras(bundle);
						startActivityForResult(In,RG_BRAND);
					}
				} catch (Exception e) {
					Log.v("choosebrand", " e=" + e.getMessage());
				}
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		this.finish();
		Log.v("choose brand","on activty result");
		
	}
	
	public void onDestroy(){
    	notedb.close();
	   super.onDestroy();
	   
	}
}